package mo.leetcode_top;

import java.util.Arrays;

public class L75 {

    public void sortColors(int[] nums) {
        int red = 0;
        int blue = nums.length - 1;
        for (int i = 0; i <= blue; i++) {
            if (nums[i] == 0) {
                swap(nums, i, red);
                red++;
            }
            if (nums[i] == 2) {
                swap(nums, i, blue);
                i--;
                blue--;
            }
        }
    }

    private void swap(int[] nums, int origin, int target) {
        int tmp = nums[origin];
        nums[origin] = nums[target];
        nums[target] = tmp;
    }


    public static void main(String[] args) {
        L75 test = new L75();
        int[] arr = new int[]{1, 2, 0};
        test.sortColors(arr);
        System.out.println(Arrays.toString(arr));
    }
}
